package ext.generic.part;

import java.util.*;

import java.sql.Timestamp;

import com.ptc.core.components.descriptor.ModelContext;
import com.ptc.core.components.factory.AbstractDataUtility;
import com.ptc.core.components.rendering.guicomponents.*;
import com.ptc.core.components.rendering.AbstractGuiComponent;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmURLFactoryBean;
import com.ptc.netmarkets.util.misc.*;
import com.ptc.core.ui.resources.ComponentMode;

import wt.epm.EPMDocument;

import wt.fc.ObjectIdentifier;
import wt.fc.WTObject;
import wt.fc.collections.WTCollection;

import wt.session.SessionHelper;

import wt.util.*;

import wt.workflow.work.WorkItem;
import wt.workflow.work.WfAssignedActivity;

/**
 * 该类专用于定制的JSP页面显示或设置是否需要打印签审图的选项
 */
public class WfPrintInfoDataUtility extends AbstractDataUtility
{

    public static boolean VERBOSE = true;
    public static String RESOURCE = "ext.generic.part.partResource";
    public static WTProperties wtProperties;
    public static String CLASSNAME = "";
    public static String PrintDraftFlagPrifix = "NeedPrintDraftFlag_";

    static
    {
        CLASSNAME = WfPrintInfoDataUtility.class.getName();
        try
        {
            wtProperties = WTProperties.getLocalProperties();
            VERBOSE = wtProperties.getProperty("ext.generic.part.verbose", false);
        }
        catch (java.io.IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

    public WfPrintInfoDataUtility()
    {
    }

    public Object getDataValue(String s, Object obj, ModelContext modelcontext) throws WTException
    {
        Object obj1 = null;

        if (s.equals("setNeedPrintDraftFlag"))
        {
            WTObject printObject = (WTObject) obj;
            obj1 = getNeedPrintDraftCheckbox(printObject, modelcontext);
        }

        else if (s.equals("needPrintDraft"))
        {
            WTObject printObject = (WTObject) obj;
            obj1 = getNeedPrintDraftInfo(printObject, modelcontext);
        }

        return obj1;
    }

    /**
     * 输出用户已选择的“是否打印签审图”的值;该信息来源于流程变量
     */
    public AbstractGuiComponent getNeedPrintDraftCheckbox(WTObject obj, ModelContext modelcontex) throws WTException
    {
        if (VERBOSE)
            System.out.println(">>>" + CLASSNAME + ".getNeedPrintDraftCheckbox()");
        AbstractGuiComponent obj1 = null;
        NmOid actionOid = modelcontex.getNmCommandBean().getActionOid();
        WorkItem workitem = (WorkItem) actionOid.getRef();
        WfAssignedActivity wfassignedactivity = (WfAssignedActivity) workitem.getSource().getObject();
        Vector printDraftObjects = (Vector) (wfassignedactivity.getContext().getValue("printDraftObjects"));
        if (VERBOSE)
        {
            System.out.println("    printDraftObjects=" + printDraftObjects);
        }
        String printFlag = "";
        if (printDraftObjects != null)
        {
            if (obj instanceof EPMDocument)
            {
                CheckBox checkBox = new CheckBox();
                String masterOid = ((EPMDocument) obj).getMaster().toString();
                checkBox.setId(PrintDraftFlagPrifix + masterOid);
                checkBox.setName(PrintDraftFlagPrifix + masterOid);
                if (printDraftObjects.contains(masterOid))
                    checkBox.setChecked(true);
                else
                    checkBox.setChecked(false);

                checkBox.setEditable(true);
                checkBox.setEnabled(true);
                obj1 = checkBox;

                if (VERBOSE)
                    System.out.println("	checkbox is generated.");
            }
            else
                obj1 = new Label("");
        }
        else
            obj1 = new Label("");
        if (VERBOSE)
            System.out.println("<<<" + CLASSNAME + ".getNeedPrintDraftCheckbox()");
        return obj1;
    }

    /**
     * 输出用户已选择的“是否打印签审图”的值;该信息来源于流程变量
     */
    public AbstractGuiComponent getNeedPrintDraftInfo(WTObject obj, ModelContext modelcontex) throws WTException
    {
        AbstractGuiComponent obj1 = null;
        NmOid actionOid = modelcontex.getNmCommandBean().getActionOid();
        WorkItem workitem = (WorkItem) actionOid.getRef();
        WfAssignedActivity wfassignedactivity = (WfAssignedActivity) workitem.getSource().getObject();
        Vector printDraftObjects = (Vector) (wfassignedactivity.getContext().getValue("printDraftObjects"));
        if (VERBOSE)
        {
            System.out.println("	printDraftObjects=" + printDraftObjects);
        }
        String printFlag = "";
        if (printDraftObjects != null)
        {
            if (obj instanceof EPMDocument)
            {
                if (printDraftObjects.contains(((EPMDocument) obj).getMaster().toString()))
                    printFlag = "YES";
            }
        }
        obj1 = new Label(printFlag);
        return obj1;
    }
}
